package com.ag.wsq.dao;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.ag.bean.TbSched;
import com.ag.dao.TbSchedDAO;

public class SchedDAO extends TbSchedDAO {
	
	private static final Log log = LogFactory.getLog(SchedDAO.class);
	
	public TbSched getById(String UId) {
		log.debug("getting TbSched by id: " + UId);
		try {
			String hql = "from TbSched s join fetch s.tbDuty " +
					"join fetch s.tbDetailShift join fetch s.tbUser where s.UId = " + UId;
			TbSched instance = (TbSched) getHibernateTemplate().find(hql).get(0);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}
	
	/*public TbSched getOneSched(String scTime) {
		log.debug("getting TbSched by scTime: " + scTime);
		try {
			String hql = "from TbSched s where s.id.scTime = " + scTime;
			TbSched instance = (TbSched) getHibernateTemplate().find(hql);
			log.info("insance----------");
			System.out.println(instance+"-----------");
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}*/
	public List getOneSched(final String cql,  final String timeStart,  final String timeEnd)
		throws Exception {
			return getHibernateTemplate().executeFind(new HibernateCallback() {
				public Object doInHibernate(Session s) throws HibernateException,
						SQLException {
					Query query = s.createQuery(cql);
					if(timeStart!=null&&timeStart.length()>0){
						java.sql.Date begin = java.sql.Date.valueOf(timeStart);
						query.setDate("timeStart", begin);
					}
					if(timeEnd!=null&&timeEnd.length()>0) {
						java.sql.Date end = java.sql.Date.valueOf(timeEnd);
						query.setDate("timeEnd",end);
					}
					List list = query.list();
					return list;
				}
			});
	}
	
	public TbSched getByName(String name) {
		log.debug("getting TbSched by name: " + name);
		try {
			String hql = "from TbSched s join fetch s.tbDuty " +
					"join fetch s.tbDetailShift join fetch s.tbUser where s.tbUser.UName = " + name;
			TbSched instance = (TbSched) getHibernateTemplate().find(hql).get(0);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}
	
	public List findAll(){
		log.debug("finding all TbSched instances");
		try {
			String queryString = "from TbSched";
		 	return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	
	public List queryCount(final String cql,  final String timeStart,  final String timeEnd)
			throws Exception {
		return getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session s) throws HibernateException,
					SQLException {
				Query query = s.createQuery(cql);
				if(timeStart!=null&&timeStart.length()>0){
					java.sql.Date begin = java.sql.Date.valueOf(timeStart);
					query.setDate("timeStart", begin);
				}
				if(timeEnd!=null&&timeEnd.length()>0) {
					java.sql.Date end = java.sql.Date.valueOf(timeEnd);
					query.setDate("timeEnd",end);
				}
				List list = query.list();
				return list;
			}
		});
	}
	
	/*public List query(final String hsql, final String timeStart,final String timeEnd, final int firstRow, final int maxRow)
			throws Exception {
		return getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session s) throws HibernateException,
					SQLException {
				Query query = s.createQuery(hsql);
				if(timeStart!=null&&timeStart.length()>0){
					java.sql.Date begin = java.sql.Date.valueOf(timeStart);
					query.setDate("timeStart", begin);
				}
				if(timeEnd!=null&&timeEnd.length()>0) {
					java.sql.Date end = java.sql.Date.valueOf(timeEnd);
					query.setDate("timeEnd",end);
				}
				query.setFirstResult(firstRow);
				query.setMaxResults(maxRow);
				List list = query.list();
				return list;
			}
		});
	}*/

}
